package com.ruoyi.web.controller.ingredients;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.domain.ingredients.PartIngredients;
import com.ruoyi.system.domain.ingredients.PartIngredientsDetail;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.ingredientsService.IPartIngredientsDetailService;
import com.ruoyi.system.service.ingredientsService.IPartIngredientsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 辅料出入库明细Controller
 * 
 * @author ljx
 * @date 2020-07-01
 */
@Controller
@RequestMapping("/system/detail")
public class PartIngredientsDetailController extends BaseController
{
    private String prefix = "system/detail";

    @Autowired
    private IPartIngredientsDetailService partIngredientsDetailService;
    @Resource
    private IPartIngredientsService iPartIngredientsService;
    @Resource
    private ISysUserService iSysUserService;

    @RequiresPermissions("system:detail:view")
    @GetMapping("/{id}")
    public String detail(@PathVariable("id") Long id,ModelMap mmap)
    {
        PartIngredients partIngredients = iPartIngredientsService.selectPartIngredientsById(id);
        mmap.put("partIngredients",partIngredients);
        return prefix + "/detail";
    }

    /**
     * 查询辅料出入库明细列表
     */
    @RequiresPermissions("system:detail:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PartIngredientsDetail partIngredientsDetail)
    {
        startPage();
        List<PartIngredientsDetail> list = partIngredientsDetailService.selectPartIngredientsDetailList(partIngredientsDetail);
        return getDataTable(list);
    }

    /**
     * 导出辅料出入库明细列表
     */
    @RequiresPermissions("system:detail:export")
    @Log(title = "辅料出入库明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PartIngredientsDetail partIngredientsDetail)
    {
        List<PartIngredientsDetail> list = partIngredientsDetailService.selectPartIngredientsDetailList(partIngredientsDetail);
        ExcelUtil<PartIngredientsDetail> util = new ExcelUtil<PartIngredientsDetail>(PartIngredientsDetail.class);
        return util.exportExcel(list, "detail");
    }

    /**
     * 新增辅料出库明细
     */
    @GetMapping("/outStore/{id}")
    public String outStore(@PathVariable("id") Long id,ModelMap mmap)
    {
        PartIngredients partIngredients = iPartIngredientsService.selectPartIngredientsById(id);
        List<SysUser> users = iSysUserService.getAllUser();
        mmap.put("partIngredients",partIngredients);
        mmap.put("users",users);
        return prefix + "/outStore";
    }

    /**
     * 新增保存辅料出库明细
     */
    @Log(title = "辅料出入库明细", businessType = BusinessType.INSERT)
    @PostMapping("/outStore")
    @ResponseBody
    public AjaxResult outStoreSave(PartIngredientsDetail partIngredientsDetail)
    {
    	Long changeValue = partIngredientsDetail.getChangeValue();
    	if (changeValue == null) {
			return AjaxResult.error("没传数量");
		}
    	if (changeValue.longValue() == 0l) {
    		return AjaxResult.error("数量不能为0");
    	}
    	if (changeValue.longValue() > 0l) {
			changeValue = changeValue*(-1l);
		}
    	PartIngredients selectPartIngredientsById = iPartIngredientsService.selectPartIngredientsById(partIngredientsDetail.getId());
    	Long inventory = selectPartIngredientsById.getInventory();
    	if (inventory + changeValue < 0) {
    		return AjaxResult.error("库存数量最多为"+inventory);
		}
    	
        partIngredientsDetail.setPartId(partIngredientsDetail.getId());
        partIngredientsDetail.setType(1);
        partIngredientsDetail.setId(null);
        return toAjax(partIngredientsDetailService.insertPartIngredientsDetail(partIngredientsDetail));
    }

    /**
     * 新增辅料出库明细
     */
    @GetMapping("/inStore/{id}")
    public String inStore(@PathVariable("id") Long id,ModelMap mmap)
    {
        PartIngredients partIngredients = iPartIngredientsService.selectPartIngredientsById(id);
        List<SysUser> users = iSysUserService.getAllUser();
        mmap.put("partIngredients",partIngredients);
        mmap.put("users",users);
        return prefix + "/inStore";
    }

    /**
     * 新增保存辅料出库明细
     */
    @Log(title = "辅料出入库明细", businessType = BusinessType.INSERT)
    @PostMapping("/inStore")
    @ResponseBody
    public AjaxResult inStoreSave(PartIngredientsDetail partIngredientsDetail)
    {
        partIngredientsDetail.setPartId(partIngredientsDetail.getId());
        partIngredientsDetail.setType(2);
        partIngredientsDetail.setId(null);
        partIngredientsDetail.setReceiver(ShiroUtils.getSysUser().getUserName());
        return toAjax(partIngredientsDetailService.insertPartIngredientsDetail(partIngredientsDetail));
    }

    /**
     * 修改辅料出入库明细
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        PartIngredientsDetail partIngredientsDetail = partIngredientsDetailService.selectPartIngredientsDetailById(id);
        mmap.put("partIngredientsDetail", partIngredientsDetail);
        return prefix + "/edit";
    }

    /**
     * 修改保存辅料出入库明细
     */
    @RequiresPermissions("system:detail:edit")
    @Log(title = "辅料出入库明细", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PartIngredientsDetail partIngredientsDetail)
    {
        return toAjax(partIngredientsDetailService.updatePartIngredientsDetail(partIngredientsDetail));
    }

    /**
     * 删除辅料出入库明细
     */
    @RequiresPermissions("system:detail:remove")
    @Log(title = "辅料出入库明细", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(partIngredientsDetailService.deletePartIngredientsDetailByIds(ids));
    }
}
